var app = getApp();
var pageIndex, pageSize, hasNext;
var infoList=[];
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    pageIndex = 1;
    pageSize = 10;
    infoList=[];
    //banner
    wx.request({
      url: app.getUrl() +'/api/wechat_news/banner_info',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res){
        if (res && res.statusCode == 200 && res.data && res.data.status==200){
          that.setData({
            imgUrls:res.data.obj
          })
        }else{
          $Toast({
            content: res.data.msg,
            type: 'warning',
            duration: 2,
          });
        }
      },
      fail(res){
        console.log(res)
        $Toast({
          content: '连接服务器失败',
          type: 'error',
          duration: 2,
        });
      }
    })
    that.getNewsInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (hasNext) {
      pageIndex++;
      this.getNewsInfo();
    } else {
      $Toast({
        content: '暂无更多',
        type: 'warning',
        duration: 2,
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getNewsInfo(){
    var that=this;
    $Toast({
      content: '请稍候',
      type: 'loading',
      duration: 0,
    });
    //资讯列表
    wx.request({
      url: app.getUrl() + '/api/wechat_news/wechat_news_infos',
      data:{
        pageIndex: pageIndex,
        pageSize: pageSize
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          $Toast.hide()
          hasNext = res.data.obj.hasNext;
          infoList = infoList.concat(res.data.obj.rows);
          that.setData({
            list: infoList,
          })
        } else {
          $Toast({
            content: res.data.msg,
            type: 'warning',
            duration: 2,
          });
        }
      },
      fail(res) {
        console.log(res)
        $Toast({
          content: '连接服务器失败',
          type: 'error',
          duration: 2,
        });
      }
    })
  },
  selectProduct(e){
    wx.navigateTo({
      url: '../productNotice/productNotice?id=' + e.currentTarget.dataset.id,
    })
  }
})